package cn.com.gridinfo.sooc.core.expand.callname.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.expand.callname.bean.Student;

/**
 * 签到照片
 * Created by yingchunli on 2015/12/23.
 */
public class SignPhotoAdapter extends BaseAdapter {

    private List<Student> photoList;
    private Context context;

    public SignPhotoAdapter(List<Student> photoList, Context context) {
        this.photoList = photoList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return photoList.size();
    }

    @Override
    public Object getItem(int position) {
        return photoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        convertView = LayoutInflater.from(context).inflate(R.layout.expand_signinfo_signphoto_item, null);

        ImageView imageView = (ImageView) convertView.findViewById(R.id.img_signphoto);//签到图片
        TextView name = (TextView) convertView.findViewById(R.id.name);//姓名
        TextView xh = (TextView) convertView.findViewById(R.id.xh);//学号
        FrameLayout quexi_tag = (FrameLayout) convertView.findViewById(R.id.quexi_tag);//缺席标识
        TextView tag = (TextView) convertView.findViewById(R.id.tag);//缺席

        name.setText(photoList.get(position).getXm());
        xh.setText(photoList.get(position).getXh());

        //签到方式   0输入课堂号 1扫码 2老师补签    //  qdzt 1:到席  2:缺席

        //有图片时
        if (photoList.get(position).getPicpath() != null && !photoList.get(position).getPicpath().isEmpty()) {
            Picasso.with(context).load(photoList.get(position).getPicpath()).placeholder(R.mipmap.signphoto_zhan)
                    .error(R.mipmap.signphoto_zhan).resize(300, 280).centerCrop().into(imageView);
            //缺席
            if (photoList.get(position).getQdzt().equals("2")) {
                //设置姓名和学号为红色字
                quexi_tag.setVisibility(View.VISIBLE);
                name.setTextColor(context.getResources().getColor(R.color.red_hint));
                xh.setTextColor(context.getResources().getColor(R.color.red_hint));
                tag.setText("缺席");
            } else {
                //补签
                if (photoList.get(position).getQdfs().equals("2")) {
                    //设置补签标签
                    quexi_tag.setVisibility(View.VISIBLE);
                    name.setTextColor(context.getResources().getColor(R.color.color_tv333));
                    xh.setTextColor(context.getResources().getColor(R.color.color_tv333));
                    tag.setText("补签");
                }
            }
        } else {
            if (photoList.get(position).getQdzt().equals("1")) {
                //没有图片时，显示补签图片
                Picasso.with(context).load(R.mipmap.expand_callname_buqian_small).resize(300, 280).centerCrop().into(imageView);
            }
        }

        //图片的点击事件
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClick(photoList.get(position), position);
            }
        });
        return convertView;
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 接口  点击item
     */
    public interface OnItemClickListener {
        void onClick(Student student, int position);
    }
}
